package com.breadenglish.cms.api.app.sys.manager;

import com.breadenglish.cms.api.app.common.anotation.EnablePaging;
import com.breadenglish.cms.api.app.common.context.PagingContextHolder;
import com.breadenglish.cms.api.app.common.page.Page;
import com.breadenglish.cms.api.app.common.page.QueryPage;
import com.breadenglish.cms.api.app.domain.constants.MessageConstants;
import com.breadenglish.cms.api.app.domain.db.Code;
import com.breadenglish.cms.api.app.domain.db.CodeClass;
import com.breadenglish.cms.api.app.domain.model.ResultMessage;
import com.breadenglish.cms.api.app.domain.query.CodeQuery;
import com.breadenglish.cms.api.app.sys.service.CodeClassService;
import com.breadenglish.cms.api.app.sys.service.CodeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典管理
 * User: guoyao
 * Date: 13-12-17
 * Time: 上午9:18
 */
@Controller
@RequestMapping("/base")
public class CodeController {

    private final static Logger logger = LoggerFactory.getLogger(CodeController.class);

    @Resource
    private CodeService codeService;

    @Resource
    private CodeClassService codeClassService;

    /**
     * 字典管理主页面
     * @param
     * @return
     */
    @RequestMapping(value="/forwardQueryCodes.html")
    public String forwardQueryCodes() throws Throwable {
        return "base/code/query_codes";
    }

    /**
     * 显示字典信息页面
     * @param
     * @return
     */
    @RequestMapping(value = "/displayCodeTemplate.html")
    public ModelAndView displayCodeTemplate(@ModelAttribute("codeQuery") CodeQuery codeQuery) throws Throwable {
        ModelAndView mav = new ModelAndView();
        mav.addObject("codeInfo",codeService.queryCode(codeQuery));
        mav.setViewName("base/code/display_code");
        return mav;
    }

    /**
     * 添加字典信息页面
     * @param
     * @return
     */
    @RequestMapping(value = "/insertCodeTemplate.html")
    public String insertCodeTemplate() throws Throwable {
        return "base/code/insert_code";
    }

    /**
     * 更新字典信息页面
     * @param
     * @return
     */
    @RequestMapping(value = "/updateCodeTemplate.html")
    public ModelAndView updateCodeTemplate(@ModelAttribute("codeQuery") CodeQuery codeQuery) throws Throwable {
        ModelAndView mav = new ModelAndView();
        mav.addObject("codeInfo",codeService.queryCode(codeQuery));
        mav.setViewName("base/code/update_code");
        return mav;
    }

    /**
     * 查询字典分类列表
     * @param codeClass 字典信息
     * @return 返回结果
     */
    @RequestMapping(value = "/queryCodeClassesForSelectMenu.html")
    @ResponseBody
    public Map<String,Object> queryCodeClassesForSelectMenu(@ModelAttribute("codeClass") CodeClass codeClass) throws Throwable {
        List<CodeClass> result = codeClassService.queryCodeClasssForSelectMenu(codeClass);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("rows",result);
        return resultMap;
    }

    /**
     * 查询所有字典信息，生成字典树
     * @param code
     * @return 返回结果
     */
    @RequestMapping(value = "/queryCodesForTree.html")
    @ResponseBody
    public List<Code> queryCodesForTree(@ModelAttribute("code") Code code) throws Throwable {
        return codeService.queryCodeTree(code);
    }

    /**
     * 字典搜索
     * @param queryPage 分页信息
     * @param code          字典信息
     * @return 返回结果
     */
    @EnablePaging
    @RequestMapping(value="/queryCodes.html")
    @ResponseBody
    public Map<String,Object> queryCodes(@ModelAttribute("queryPage") QueryPage queryPage,@ModelAttribute("code") CodeQuery code) throws Throwable {
        List<CodeQuery> result = codeService.selectCodeAndClass(code);
        Page page = PagingContextHolder.getPage();
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("total",page.getTotalResult());
        resultMap.put("rows",result);
        return resultMap;
    }

    /**
     * 添加字典信息
     * @param code 字典信息
     * @return 添加结果
     */
    @RequestMapping(value="/insertCode.html")
    @ResponseBody
    public ResultMessage insertCode(@ModelAttribute("code") Code code) throws Throwable {
        codeService.insertCode(code);
        return CommonUtils.createResultMessage(MessageConstants.INSERT_CODE_SUCCESS);
    }

    /**
     * 更新字典信息
     * @param code 字典信息
     * @return 返回结果
     */
    @RequestMapping(value="/updateCode.html")
    @ResponseBody
    public ResultMessage updateCode(@ModelAttribute("code") Code code) throws Throwable {
        codeService.updateCode(code);
        return CommonUtils.createResultMessage(MessageConstants.UPDATE_CODE_SUCCESS);
    }

    /**
     * 删除字典信息
     * @param code 字典信息
     * @param codeId 字典ID
     * @param classId 字典分类ID
     * @return 返回结果
     */
    @RequestMapping(value="/deleteCodes.html")
    @ResponseBody
    public ResultMessage deleteCodes(@ModelAttribute("code") Code code,@RequestParam("delCodeIds") String codeId,@RequestParam("delClassIds") String classId) throws Throwable {
        codeService.deleteCode(code,codeId,classId);
        return CommonUtils.createResultMessage(MessageConstants.DELETE_CODE_SUCCESS);
    }

    /**
     * 验证字典信息唯一性
     * @param codeId 字典ID
     * @param classId 分类ID
     * @return 返回结果
     */
    @RequestMapping(value="/checkCode.html")
    @ResponseBody
    public ResultMessage checkCodeUnique(@RequestParam("codeId") String codeId, @RequestParam("classId") String classId) throws Throwable {
        codeService.uniqueCode(codeId,classId);
        return CommonUtils.createResultMessage(MessageConstants.CODE_UNIQUE_CHECK_SUCCESS);
    }

}
